home *** CD-ROM | disk | FTP | other *** search
/ Java Interactive Reference Guide / Java Interactive Reference Guide.iso / autorun / source.dir / 00120_15.txt < prev    next >
Encoding:
Text File  |  1980-01-11  |  17.9 KB  |  570 lines

  1. /*
  2.  * @(#)WordMatch.java    1.5  25 Oct 1995 11:07:56
  3.  * @author Patrick Chan
  4.  *
  5.  * Copyright (c) 1994-1995 Sun Microsystems, Inc. All Rights Reserved.
  6.  *
  7.  * Permission to use, copy, modify, and distribute this software
  8.  * and its documentation for NON-COMMERCIAL or COMMERCIAL purposes and
  9.  * without fee is hereby granted. 
  10.  * Please refer to the file http://java.sun.com/copy_trademarks.html
  11.  * for further important copyright and trademark information and to
  12.  * http://java.sun.com/licensing.html for further important licensing
  13.  * information for the Java (tm) Technology.
  14.  * 
  15.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  16.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  17.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  18.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  19.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  20.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  21.  * 
  22.  * THIS SOFTWARE IS NOT DESIGNED OR INTENDED FOR USE OR RESALE AS ON-LINE
  23.  * CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING FAIL-SAFE
  24.  * PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, AIRCRAFT
  25.  * NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT LIFE
  26.  * SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE
  27.  * SOFTWARE COULD LEAD DIRECTLY TO DEATH, PERSONAL INJURY, OR SEVERE
  28.  * PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH RISK ACTIVITIES").  SUN
  29.  * SPECIFICALLY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR
  30.  * HIGH RISK ACTIVITIES.
  31.  */
  32.  
  33. import java.awt.*;
  34. import java.awt.image.ImageObserver;
  35.  
  36. public class WordMatch extends java.applet.Applet {
  37.     /* Number of displayed cards and word. */
  38.     int numCards = 4;
  39.  
  40.     /* Currently selected language. */
  41.     int curLanguage;
  42.  
  43.     /* Width and size().height of language box. */
  44.     int languageBoxW, languageBoxH;
  45.  
  46.     /* Size of border around language box. */
  47.     int languageBoxB;
  48.  
  49.     /* Line spacing for wordFont. */
  50.     int languageBoxSp;
  51.  
  52.     /* Font of languageBox. */
  53.     Font languageFont;
  54.     FontMetrics languageFontMt;
  55.  
  56.     /** 
  57.      * Cards
  58.      */ 
  59.  
  60.     /* card[i] refers to the i'th word. */
  61.     int cards[];
  62.  
  63.     /* Currently selected card. */
  64.     int curCard;
  65.  
  66.     /* Size of cards. */
  67.     int cardW, cardH;
  68.  
  69.     /* Location and size of card box. */
  70.     int cardBoxX, cardBoxY, cardBoxW, cardBoxH;
  71.  
  72.     /* Sp between cards. */
  73.     int cardBoxSp;
  74.  
  75.     /** 
  76.      * Sound box
  77.      */ 
  78.  
  79.     /* Location of sound box. */
  80.     int soundBoxX, soundBoxY;
  81.  
  82.     /* Sound image. */
  83.     Image soundImage;
  84.     int soundImageWidth = 32;
  85.     int soundImageHeight = 32;
  86.  
  87.     /** 
  88.      * Words
  89.      */ 
  90.  
  91.     /* word[i] refers to the i'th card. */
  92.     int words[];
  93.  
  94.     /* Location and size of word box. */
  95.     int wordBoxX, wordBoxY, wordBoxW;
  96.  
  97.     /* Font used to paint words.. */
  98.     Font wordFont;
  99.     FontMetrics wordFontMt;
  100.  
  101.     /**
  102.      * Scoring
  103.      */
  104.  
  105.     /* Location and size of score box. */
  106.     int scoreBoxX, scoreBoxY, scoreBoxW, scoreBoxH;
  107.  
  108.     /* Message to be displayed next to the score box. */
  109.     String msg;
  110.     
  111.     /* If true, the user has successfully matched all the words. */
  112.     boolean done;
  113.     
  114.     /** 
  115.      * Links
  116.      */ 
  117.  
  118.     /* links[i] == j -> cards[i] is linked to words[j]. */
  119.     int links[];
  120.  
  121.     /** 
  122.      * Misc
  123.      */ 
  124.  
  125.     /* Images of pictures.  There is one image for each different word. 
  126.        -1 => not loaded. */
  127.     Image images[];
  128.     int imageWidth[];
  129.     int imageHeight[];
  130.  
  131.     /* Average size().width and size().height of an M character in the font. */
  132.     int charW, charH;
  133.  
  134.     /**
  135.      * Initialize the applet. Resize and load images.
  136.      */
  137.     public void init() {
  138.         int maxWidth = 0;
  139.  
  140.         cards = new int[numCards];
  141.         words = new int[numCards];
  142.         links = new int[numCards];
  143.         images = new Image[englishWords.length];
  144.         imageWidth = new int[englishWords.length];
  145.         imageHeight = new int[englishWords.length];
  146.         curLanguage = 0;
  147.  
  148.         // Setup the language selection box.
  149.         languageFont = new java.awt.Font("Helvetica", Font.PLAIN, 14);
  150.         languageFontMt = getFontMetrics(languageFont);
  151.         languageBoxSp = 3;
  152.  
  153.         for (int i=0; i<languages.length; i++) {
  154.             int w = languageFontMt.stringWidth(languages[i]);
  155.             if (w > maxWidth) {
  156.                 maxWidth = w;
  157.             }
  158.         }
  159.         languageBoxB = 2;
  160.         languageBoxW = maxWidth + 2 * languageFontMt.charWidth('M') 
  161.                         + 2 * languageBoxB;
  162.         languageBoxH = languages.length
  163.             * (languageFontMt.getHeight() + languageBoxSp) 
  164.             + languageBoxSp + 2 * languageBoxB;
  165.  
  166.     cardW = cardH = 64;
  167.         cardBoxSp = 5;
  168.         cardBoxX = languageBoxW + cardW;
  169.         cardBoxY = 0;
  170.         cardBoxW = cardW;
  171.         cardBoxH = (cardH + cardBoxSp) * (numCards - 1) + cardH;
  172.  
  173.         // Setup sound box.
  174.         soundImage = getImage(getCodeBase(), "rsrc/sound.gif");
  175.         soundBoxX = (languageBoxW - soundImageWidth) / 2;
  176.         soundBoxY = languageBoxH + 10;
  177.         soundBoxX = languageBoxW + 10;
  178.         soundBoxY = (languageBoxH/2);
  179.  
  180.         // Setup word box.
  181.         wordFont = new java.awt.Font("Helvetica", Font.PLAIN, 18);
  182.         wordFontMt = getFontMetrics(wordFont);
  183.         charW = wordFontMt.charWidth('M');
  184.         charH = wordFontMt.getHeight();
  185.         wordBoxX = cardBoxX + cardBoxW * 2;
  186.         wordBoxY = 0;
  187.         wordBoxW = 0;
  188.         for (int i=0; i<languages.length; i++) {
  189.             for (int j=0; j<englishWords.length; j++) {
  190.                 int w = wordFontMt.stringWidth(dictionary[i][j]);
  191.                 if (w > wordBoxW) {
  192.                     wordBoxW = w;
  193.                 }
  194.             }
  195.         }
  196.         wordBoxW += 2 * charW;
  197.         
  198.         // Setup score box.
  199.         scoreBoxX = 0;
  200.         scoreBoxY = languageBoxH + charH;
  201.         scoreBoxW = wordFontMt.stringWidth("Score") + 2 * charW;
  202.         scoreBoxH = charH + 4;
  203.         newRound();
  204.  
  205.         resize(wordBoxX + wordBoxW + 1, scoreBoxY + scoreBoxH + 1);
  206.     }
  207.  
  208.     public boolean imageUpdate(Image img, int infoflags,
  209.                    int x, int y, int width, int height) {
  210.         if ((infoflags & (WIDTH | HEIGHT)) != 0) {
  211.             // This is a waste of cycles.
  212.             // Maybe I should use a hash table.
  213.         for (int i=0; i<images.length; ++i) {
  214.         if (img == images[i]) {
  215.                     if ((infoflags & WIDTH) != 0) {
  216.                         imageWidth[i] = width;
  217.                     }
  218.                     if ((infoflags & HEIGHT) != 0) {
  219.                         imageHeight[i] = height;
  220.                     }
  221.                 }
  222.             }
  223.         }
  224.         return super.imageUpdate(img, infoflags, x, y, width, height);
  225.     }
  226.  
  227.     /**
  228.      * Paint the screen.
  229.      */
  230.     public void paint(Graphics g) {
  231.         int x, y;
  232.  
  233.         // Paint language box.
  234.         g.setFont(languageFont);
  235.         y = languageFontMt.getHeight() + languageBoxB;
  236.         for (int i=0; i<languages.length; i++) {
  237.             if (i == curLanguage) {
  238.                 g.setColor(Color.red);
  239.             } else {
  240.         g.setColor(Color.black);
  241.             } 
  242.             x = languageFontMt.stringWidth(languages[i]);
  243.             x = (languageBoxW - x) / 2 + languageBoxB;
  244.             g.drawString(languages[i], x, y);
  245.             y += languageFontMt.getHeight() + languageBoxSp;
  246.         }
  247.     g.setColor(Color.black);
  248.         g.draw3DRect(0, 0, languageBoxW, languageBoxH, true);
  249.         
  250.         // Paint sound box.
  251.     if (soundImage != null
  252.             && !languages[curLanguage].equals("Geekspeak")) {
  253.         g.drawImage(soundImage, soundBoxX, soundBoxY, this);
  254.         }
  255.  
  256.         // Paint words.
  257.         g.setFont(wordFont);
  258.         x = wordBoxX;
  259.         y = (cardH - charH) / 2 + charH;
  260.         for (int i=0; i<numCards; i++) {
  261.             g.drawString(dictionary[curLanguage][words[i]], x, y);
  262.             y += cardH + cardBoxSp;
  263.         }
  264.  
  265.         // Paint links.
  266.         g.setColor(Color.black);
  267.         for (int i=0; i<numCards; i++) {
  268.             if (links[i] >= 0) {
  269.                 int y2 = links[i] * (cardH + cardBoxSp) + cardH / 2;
  270.  
  271.         y = i * (cardH + cardBoxSp) + cardH / 2;
  272.                 g.drawLine(cardBoxX + cardBoxW + 2, y,
  273.                     wordBoxX - 2, y2);
  274.             }
  275.     }
  276.  
  277.         // Paint score box.
  278.         g.draw3DRect(scoreBoxX, scoreBoxY, scoreBoxW, scoreBoxH, true);
  279.         if (done) {
  280.         g.drawString("New", scoreBoxX + charW, scoreBoxY + charH);
  281.         } else {
  282.         g.drawString("Score", scoreBoxX + charW, scoreBoxY + charH);
  283.         }
  284.         if (msg != null) {
  285.             g.setFont(new java.awt.Font("Helvetica", Font.PLAIN, 16));
  286.             g.setColor(Color.blue);
  287.             g.drawString(msg, scoreBoxW + charW, scoreBoxY + charH);
  288.             g.setFont(wordFont);
  289.         }
  290.     
  291.         // Paint cards.
  292.         x = cardBoxX;
  293.         y = 0;
  294.         for (int i=0; i<numCards; i++) {
  295.         if (images[cards[i]] == null) {
  296.                 // need to load the image.
  297.         images[cards[i]] = getImage(getCodeBase(), "rsrc/"
  298.                     + englishWords[cards[i]] + ".gif");
  299.                 images[cards[i]].getWidth(this);
  300.                 images[cards[i]].getHeight(this);
  301.             } 
  302.             if (images[cards[i]] == null) {
  303.         g.draw3DRect(x, y, cardW, cardH, true);
  304.             } else if (imageWidth[cards[i]]>0 && imageHeight[cards[i]]>0) {
  305.                 int offsetX = (cardW - imageWidth[cards[i]]) / 2;
  306.                 int offsetY = (cardH - imageHeight[cards[i]]) / 2;
  307.  
  308.         g.drawImage(images[cards[i]], x + offsetX, y + offsetY, this);
  309.             }
  310.  
  311.             if (i == curCard) {
  312.                 g.setColor(Color.red);
  313.                 g.drawRect(x, y, cardW, cardH);
  314.                 g.setColor(Color.black);
  315.             }
  316.             y += cardH + cardBoxSp;
  317.         }
  318.     }
  319.  
  320.     /**
  321.      * mouseDown
  322.      */
  323.     public boolean mouseDown(java.awt.Event evt, int x, int y) {
  324.     int sel;
  325.  
  326.         if (!done) {
  327.             msg = null;
  328.         }
  329.         if (x < languageBoxW && y < languageBoxH) {
  330.         // In language box.
  331.             y -= languageBoxB;
  332.             sel = y / (languageFontMt.getHeight() + languageBoxSp);
  333.         sel = Math.min(sel, languages.length - 1);
  334.             if (curLanguage == sel) {
  335.         newRound();
  336.             }
  337.         curLanguage = sel;
  338.         repaint();
  339.         } else if (x > soundBoxX && y > soundBoxY 
  340.                     && x < soundBoxX + soundImageWidth
  341.                     && y < soundBoxY + soundImageHeight) {
  342.         // In sound box.
  343.             if (languages[curLanguage].equals("Geekspeak")) {
  344.                 // No voices for these languages yet.
  345.                 play(getCodeBase(), "rsrc/ding.au");
  346.             } else {
  347.         play(getCodeBase(), "rsrc/" + englishWords[cards[curCard]] 
  348.             + "." + languages[curLanguage] + ".au");
  349.             }
  350.         } else if (x > cardBoxX && y > cardBoxY 
  351.                     && x < cardBoxX + cardBoxW
  352.                     && y < cardBoxY + cardBoxH) {
  353.         // In card box.
  354.             y -= cardBoxY;
  355.             sel = y / (cardH + cardBoxSp);
  356.         sel = Math.min(sel, numCards - 1);
  357.         if (sel != curCard) {
  358.         curCard = sel;
  359.         repaint();
  360.         }
  361.         } else if (!done && x > wordBoxX && y > wordBoxY 
  362.                     && x < wordBoxX + wordBoxW
  363.                     && y < wordBoxY + cardBoxH) {
  364.             // In word box.
  365.             sel = y / (cardH + cardBoxSp);
  366.         sel = Math.min(sel, numCards - 1);
  367.             if (links[curCard] != sel) {
  368.                 // Break an old link if necessary.
  369.                 for (int i=0; i<numCards; i++) {
  370.                     if (links[i] == sel) {
  371.                         links[i] = -1;
  372.                     }
  373.                 }
  374.         links[curCard] = sel;
  375.         repaint();
  376.             }
  377.         } else if (x > scoreBoxX && y > scoreBoxY 
  378.                     && x < scoreBoxX + scoreBoxW
  379.                     && y < scoreBoxY + scoreBoxH) {
  380.             // In score box.
  381.             if (done) {
  382.                 newRound();
  383.                 repaint();
  384.                 return true;
  385.             }
  386.             int count = 0;
  387.             for (int i=0; i<numCards; i++) {
  388.                 if (links[i] == -1) {
  389.                     msg = "You have not yet matched all the words.";
  390.                     repaint();
  391.                     return true;
  392.                 }
  393.                 if (cards[i] == words[links[i]]) {
  394.                     count++;
  395.                 }
  396.             }
  397.             if (count == numCards) {            
  398.         msg = "Congratulations, they're all right!";
  399.                 done = true;
  400.             } else if (count == 1) {
  401.         msg = "There is only 1 correct match.";
  402.             } else if (count == 0) {
  403.         msg = "There are no correct matches.";
  404.             } else {
  405.         msg = "There are " + count + " correct matches.";
  406.             }
  407.             repaint();
  408.         }
  409.         return true;
  410.     }
  411.  
  412.     /**
  413.      * Returns an integer in the interval [0..max).
  414.      */
  415.     int random(int max) {
  416.     return (int)Math.floor(Math.random() * max);
  417.     }
  418.  
  419.     /**
  420.      * Starts a new round.
  421.      */
  422.     public void newRound() {
  423.         msg = null;
  424.         done = false;
  425.  
  426.         // Pick new cards.
  427.         for (int i=0; i<numCards; i++) {
  428.             int r;
  429.             boolean found;
  430.     
  431.         do {
  432.         r = random(englishWords.length);
  433.         found = false;
  434.         for (int j=0; j<i; j++) {
  435.             if (cards[j] == r) {
  436.             found = true;
  437.             }
  438.         }
  439.         } while (i > 0 && found);
  440.         cards[i] = r;
  441.             words[i] = r;
  442.             links[i] = -1;
  443.     }
  444.         curCard = 0;
  445.  
  446.         // Now mix up the words.
  447.         for (int i=0; i<50; i++) {
  448.             int r1 = random(numCards);
  449.             int r2 = random(numCards);
  450.             int t = words[r1];
  451.             words[r1] = words[r2];
  452.             words[r2] = t;
  453.         }
  454.     }
  455.  
  456.     /**
  457.      * Start the applet.
  458.      */
  459.     public void start() {
  460.     }
  461.  
  462.     /**
  463.      * Stop the applet.  
  464.      */
  465.     public void stop() {
  466.     }
  467.  
  468.  
  469.     /* Added by K.A. Smith 10/25/95 */
  470.     public String getAppletInfo() {
  471.        return "Author: Patrick Chan\nVersion: 1.5";
  472.     }
  473.  
  474.  
  475.     /* List of words in various languages */
  476.     String englishWords[] = {
  477.         "house", "book", "java", "lips", "airplane", "UnitedStates",
  478.         "apple", "clock", "phone", "star", "flower", "television",
  479.         "car", "dog", "bug", "bird"};
  480.     String cantoneseWords[] = {
  481.         "fohng uk", "syu", "ga fe", "seuhn", "fei gei", "meih gwok",
  482.         "pihng gwo", "jung", "dihn wa", "sing", "fa", "dihn sih",
  483.         "che", "gau", "chuhng", "jeuk"};
  484.     String dutchWords[] = {
  485.         "huis", "boek", "koffie", "lippen", "vliegtuig", "Verenigde Staten",
  486.         "appel", "klok", "telefoon", "ster", "bloem", "televisie",
  487.         "auto", "hond", "insekt", "vogel"};
  488.     String frenchWords[] = {
  489.         "maison", "livre", "cafe", "levres", "avion", "Etats Unis",
  490.         "pomme", "horloge", "telephone", "astre", "fleur", "television",
  491.         "voiture", "chien", "insecte", "oiseaux"};
  492.     String germanWords[] = {
  493.         "Haus", "Buch", "Kaffee", "Lippen", "Flugzeug", "Vereinigte Staaten",
  494.         "Apfel", "Uhr", "Telephon", "Stern", "Blume", "Fernsehgeraet",
  495.         "Auto", "Hund", "Kaefer", "Vogel"};
  496.     String geekspeakWords[] = {
  497.         "lab", "FM (of RTFM)", "Peet's", "programming languages", "wings", "www.usa.com",
  498.         "food", "MHz", "cellular", "splat (aka asterisk)", "inflorescence", "tube",
  499.         "wheels", "386", "feature", "finger"};
  500.     String hebrewWords[] = {
  501.         "ba'it", "sefer", "java", "s'fata'im", "matos", "ar'tzot ha'brit",
  502.         "tapu'ach", "sha'on", "tele'phon", "kochav", "pe'rach", "televizia",
  503.         "m'chonit", "kelev", "cha'rak", "tsipor"};
  504.     String hindiWords[] = {
  505.         "ghar", "kitab", "coffee", "honth", "hawai jahaj", "America",
  506.         "seb", "ghadi", "doorbhash", "sitara", "phool", "television",
  507.         "gaadi", "kutta", "keeda", "chidiya"};
  508.     String italianWords[] = {
  509.         "house", "book", "coffee", "lips", "airplane", "UnitedStates",
  510.         "apple", "clock", "phone", "star", "flower", "television",
  511.         "car", "dog", "bug", "bird"};
  512.     String japaneseWords[] = {
  513.         "ie", "hon", "kouhii", "kuchibiru", "hikouki", "Amerika",
  514.         "ringo", "tokei", "denwa", "hoshi", "hana", "terebi",
  515.         "kuruma", "inu", "mushi", "tori"};
  516.     String mandarinWords[] = {
  517.         "fang wu", "shu", "ka fei", "chun", "fei ji", "mei guo",
  518.         "ping guo", "zhong", "dian hua", "xing", "hua", "dian shi",
  519.         "che", "gou", "chong", "niao"};
  520.     String portugueseWords[] = {    
  521.     "casa", "livro", "cafe", "lapios", "aviao", "Estados Unidos",
  522.     "maca", "relogio", "telefone", "estrela", "flor", "televisao",
  523.     "carro", "cachorro", "bicho", "passarinho"};
  524.     String pigLatinWords[] = {
  525.         "ouse-hay", "ook-bay", "ava-jay", "ips-lay", "airplane-gay", "United-gay Ates-Stay",
  526.         "apple-gay", "ock-clay", "one-phay", "ar-stay", "ower-flay", "elevision-tay",
  527.         "ar-cay", "og-day", "ug-bay", "ird-bay"};
  528.     String spanishWords[] = {
  529.         "casa", "libro", "cafe", "labios", "avion", "Estados Unidos",
  530.         "manzana", "reloj", "telefono", "estrella", "flor", "television",
  531.         "coche", "perro", "insecto", "pajaro"};
  532.     String swissGermanWords[] = {
  533.         "Huus", "Buech", "Cafi", "Lippe", "Flugzueg", "Vereinigti Staate",
  534.         "Oepfel", "Uhr", "Telephon", "Stern", "Blueme", "Fernsehapparat",
  535.         "Auto", "Hund", "Chaefer", "Vogel"};
  536.  
  537.     /* List of languages. */
  538.     String languages[] = {
  539.         "English",
  540.         "Cantonese",
  541.         "Dutch",
  542.         "French",
  543.         "Geekspeak",
  544.     "German",
  545.     "Hebrew",
  546.     "Hindi",
  547.         "Japanese",
  548.         "Mandarin",
  549.         "PigLatin",
  550.         "Portuguese",
  551.         "Spanish",
  552.         "SwissGerman"};
  553.  
  554.     String dictionary[][] = {
  555.         englishWords,
  556.         cantoneseWords,
  557.         dutchWords,
  558.         frenchWords,
  559.         geekspeakWords,
  560.     germanWords,
  561.     hebrewWords,
  562.     hindiWords,
  563.         japaneseWords,
  564.         mandarinWords,
  565.         pigLatinWords,
  566.     portugueseWords,
  567.         spanishWords,
  568.         swissGermanWords};
  569. }
  570.